package com.sysu.ooad.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.Action;
import com.sysu.ooad.entity.Student;
import com.sysu.ooad.entity.Task;
import com.sysu.ooad.service.TaskService;

@Component
@Scope("prototype")
public class TaskStudentAction extends CommonAction {

	private List<Task> taskList;
	private TaskService taskService;
	
	private String sortResult;
	private Task task;
	
	public String getSortResult() {
		return sortResult;
	}
	public void setSortResult(String sortResult) {
		this.sortResult = sortResult;
	}
	public Task getTask() {
		return task;
	}
	public void setTask(Task task) {
		this.task = task;
	}
	public TaskService getTaskService() {
		return taskService;
	}
	@Resource
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	public List<Task> getTaskList() {
		return taskList;
	}
	public void setTaskList(List<Task> taskList) {
		this.taskList = taskList;
	}
	
	@Override
	public String execute() throws Exception {
		Student student = this.findCurrentStudent();
		taskList = taskService.findTasksByCourse(student.getCourse());
		return Action.SUCCESS;
	}
	
	public String sortHomeworks() throws Exception {
		System.out.println(sortResult);
		String[] sortResultStrArray = sortResult.split(",");
		Integer[] sortResultArray = new Integer[sortResultStrArray.length];
		for (int i = 0; i < sortResultArray.length; i++) {
			sortResultArray[i] = Integer.parseInt(sortResultStrArray[i]);
		}
		taskService.sortHomeworksOfOneTaskByLeader(task, this.findCurrentStudent(), sortResultArray);
		return null;
	}
}
